package com.ruoyi.web.controller.construction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.OrderTeam;
import com.ruoyi.system.service.construction.OrderTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工订单信息表
 *
 * @author 张少丛
 */
@RestController
@RequestMapping("/construction/orderTeam")
public class OrderTeamController extends BaseController {

    @Autowired
    private OrderTeamService orderTeamService;

    @Autowired
    private TokenService tokenService;


    /**
     * 施工队订单列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:list')")
    @RequestMapping("/list")
    public TableDataInfo list(OrderTeam orderTeam){
        startPage();
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<OrderTeam> orderTeamList = this.orderTeamService.selectOrderTeamList(orderTeam,userId);
        return getDataTable(orderTeamList);
    }

    /**
     * 订单查询
     * @param orderTeamId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:query')")
    @GetMapping({"/", "/{orderTeamId}"})
    public AjaxResult getOrderTeam(@PathVariable(value = "orderTeamId", required = false)Long orderTeamId){
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(orderTeamId))
        {
            ajax.put(AjaxResult.DATA_TAG, orderTeamService.selectOrderTeamById(orderTeamId));
        }
        return ajax;
    }

    /**
     * 添加订单信息
     * @param orderTeam
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:add')")
    @Log(title = "施工队订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OrderTeam orderTeam){
        return AjaxResult.success(orderTeamService.insertOrderTeam(orderTeam));
    }

    /**
     * 添加订单信息
     * @param orderTeam
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:edit')")
    @Log(title = "施工队订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody OrderTeam orderTeam){
        return AjaxResult.success(orderTeamService.updateOrderTeam(orderTeam));
    }

    /**
     * 删除订单
     * @param orderTeamIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:remove')")
    @Log(title = "施工队订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderTeamIds}")
    public AjaxResult remove(@PathVariable Long[] orderTeamIds){
        return AjaxResult.success(orderTeamService.deleteOrderTeamByIds(orderTeamIds));
    }

    /**
     * 取消订单
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:cancel')")
    @Log(title = "施工队订单管理", businessType = BusinessType.UPDATE)
    @GetMapping("/cancelOrderTeam/{orderTeamId}")
    public AjaxResult cancel(@PathVariable(value = "orderTeamId", required = false) Long orderTeamId){
        return AjaxResult.success(orderTeamService.cancelOrderTeam(orderTeamId));
    }


    /**
     * 确认完结订单
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:finish')")
    @Log(title = "施工队订单管理", businessType = BusinessType.UPDATE)
    @GetMapping("/finishOrderTeam/{orderTeamId}")
    public AjaxResult finish(@PathVariable(value = "orderTeamId", required = false) Long orderTeamId){
        return AjaxResult.success(orderTeamService.finishOrderTeam(orderTeamId));
    }

    /**
     * 导出订单信息
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:export')")
    @Log(title = "施工队订单管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public AjaxResult export(OrderTeam orderTeam){
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<OrderTeam> orderTeamList = this.orderTeamService.selectOrderTeamList(orderTeam,userId);
        ExcelUtil<OrderTeam> util = new ExcelUtil<OrderTeam>(OrderTeam.class);
        return util.exportExcel(orderTeamList, "施工队订单数据");
    }

    /**
     * 查询店铺列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:orderTeam:query')")
    @GetMapping("/selectSupplierList")
    public AjaxResult selectSupplierList(){
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        return AjaxResult.success(orderTeamService.selectSupplierList(userId));
    }
}
